﻿@page "/BtNotification"
@attribute [TabItemOption(Text = "蓝牙特征通知")]
@using System.ComponentModel

<a href="https://www.nuget.org/packages/BootstrapBlazor.Bluetooth#readme-body-tab"><h4>蓝牙特征通知</h4></a>
<hr />
<a href="https://github.com/densen2014/Densen.Extensions/blob/master/Demo/DemoShared/Pages/BtNotificationPage.razor"><h5>页面源码</h5></a>

<BootstrapInput @bind-Value="ServiceUuid" ShowLabel="true" />
<BootstrapInput @bind-Value="CharacteristicUuid" ShowLabel="true" />
<Switch @bind-Value="AutomaticComplement" ShowLabel="true" />
<Switch @bind-Value="AutoConnect" ShowLabel="true" />
<Switch @bind-Value="AdvertisementReceived" ShowLabel="true" />
<button class="btn btn-outline-secondary" @onclick="GetNotification ">读取通知</button>
<button class="btn btn-outline-secondary" @onclick="StopNotification ">停止读取</button>
<button class="btn btn-outline-secondary" @onclick="Scan ">扫描设备</button>

<BleNotification @ref="Notification" OnUpdateValue="OnUpdateValue" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" />
<h2 style="color:red" data-action="notificationValue" />
<pre>@message</pre>
<pre style="color:green">@statusmessage</pre>
<pre style="color:red">@errmessage</pre>

@code{
    //string? heartrateIcon;// { get => (heartrateIcon == "&#10084;" ? "&hearts;" : "&#10084;"); }

    BleNotification Notification { get; set; } = new BleNotification();

    /// <summary>
    /// 服务UUID / Service UUID
    /// </summary>
    [DisplayName("服务UUID")]
    public string ServiceUuid { get; set; } = "heart_rate";

    /// <summary>
    /// 特征UUID / Characteristic UUID
    /// </summary>
    /// <returns></returns>
    [DisplayName("特征UUID")]
    public string CharacteristicUuid { get; set; } = "heart_rate_measurement";

    /// <summary>
    /// 自动连接设备
    /// </summary>
    [DisplayName("自动连接设备")]
    public bool AutoConnect { get; set; } = true;

    /// <summary>
    /// 自动补全, 否则为转数字格式
    /// </summary>
    [DisplayName("自动补全, 否则为转数字格式")]
    public bool AutomaticComplement { get; set; }

    /// <summary>
    /// 显示扫描结果
    /// </summary>
    [DisplayName("显示扫描结果")]
    public bool AdvertisementReceived { get; set; }

    private string? message;
    private string? value;
    private string? statusmessage;
    private string? errmessage;

    private Task OnResult(string message)
    {
        this.message = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnUpdateValue(string value)
    {
        this.value = value;
        this.statusmessage = $"{value}";
        StateHasChanged();
        return Task.CompletedTask;
    }


    private Task OnUpdateStatus(BluetoothDevice device)
    {
        this.statusmessage = device.Status;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnError(string message)
    {
        this.errmessage = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    public async void GetNotification()
    {
        this.message = null;
        this.value = null;
        this.statusmessage = null;
        this.errmessage = null;
        await Notification.GetNotification(ServiceUuid, CharacteristicUuid, AutoConnect, AutomaticComplement, AdvertisementReceived);
    }

    public async void StopNotification()
    {
        await Notification.StopNotification();
    }

    public async void Scan()
    {
        await Notification.Scan();
    }

}
